package main

import (
	"context"
	"fmt"
	"github.com/golang/protobuf/ptypes/timestamp"
	"grpc-4/helper"
	"grpc-4/pb/services"
	"time"

	"google.golang.org/grpc"
	"log"
)

//需要拷贝根据 Prod.proto 生成的两个文件( Prod.pb.go Prod_grpc.pb.go )
func main() {
	//chrome页面通过get请求访问
	//http://localhost:8080/v1/prod/135?prod_area=101

	//通过rpc访问
	conn, err := grpc.Dial(":8081", grpc.WithTransportCredentials(helper.GetClientCredentials()))
	if err != nil {
		log.Fatalf("连接GRPC服务端失败 %v\n", err)
	}

	defer conn.Close()
	//prod 客户端
	prodClient := services.NewProductServiceClient(conn)
	//获取单个
	getProductStock(err, prodClient)
	//获取多个(列表
	getProductStocks(err, prodClient)
	//获取对象返回
	getProductInfo(err, prodClient)

	//order 客户端
	//使用 postman 请求, post方式, raw=json
	//http://localhost:8080/v1/orders
	/*
		{
		    "order_no": "20210927",
		    "order_money": -96,
		    "user_id": 1001,
		    "order_details": [
		        {"detail_id":20210927,"order_no":"20210927","prod_id":169,"prod_price":9.9,"prod_num":6},
		        {"detail_id":20210927,"order_no":"20210927","prod_id":174,"prod_price":19.9,"prod_num":5}
		    ]
		}
	*/
	orderClient := services.NewOrderServiceClient(conn)
	//创建订单
	newOrder(err, orderClient)

}

func newOrder(err error, orderClient services.OrderServiceClient) {
	//秒级别, Unix时间戳
	orderTime := timestamp.Timestamp{Seconds: time.Now().Unix()}
	slice := make([]*services.OrderDetail, 4, 20)
	slice[1] = &services.OrderDetail{
		DetailId:  10,
		OrderNo:   "567",
		ProdId:    196,
		ProdPrice: 9.9,
		ProdNum:   5,
	}
	orderRes, err := orderClient.NewOrder(context.Background(),
		&services.OrderRequest{
			OrderMain: &services.OrderMain{
				OrderId:      100,
				OrderNo:      "酱油1001",
				UserId:       200,
				OrderMoney:   -9.8,
				OrderTime:    &orderTime,
				OrderDetails: slice,
			},
		})

	if err != nil {
		log.Fatalf("请求GRPC服务端失败 %v\n", err)
	}
	fmt.Printf("订单状态 %v, 订单消息: %v\n", orderRes.Status, orderRes.Message)
	fmt.Println("---------------- ----------------")
}

func getProductInfo(err error, prodClient services.ProductServiceClient) {
	prodRes, err := prodClient.GetProductInfo(context.Background(),
		&services.ProdRequest{ProdId: 12})
	if err != nil {
		log.Fatalf("请求GRPC服务端失败 %v\n", err)
	}
	fmt.Println(prodRes)
	fmt.Println("---------------- ----------------")

}

func getProductStocks(err error, prodClient services.ProductServiceClient) {
	prods, err := prodClient.GetProductStocks(context.Background(),
		&services.QuerySize{Size: 3})
	if err != nil {
		log.Fatalf("请求GRPC服务端失败 %v\n", err)
	}
	for _, res := range prods.Prodres {
		fmt.Printf("商品库存为: %v\n", res.ProdStock)
	}
	fmt.Println("---------------- ----------------")
}

func getProductStock(err error, prodClient services.ProductServiceClient) {
	prodRes, err := prodClient.GetProductStock(context.Background(),
		&services.ProdRequest{ProdId: 12, ProdArea: services.ProdAreas_B})
	if err != nil {
		log.Fatalf("请求GRPC服务端失败 %v\n", err)
	}
	fmt.Println(prodRes.ProdStock)
	fmt.Println("---------------- ----------------")
}
